#include <iostream>
#include <algorithm>
using namespace std;

const int N = 1e3 + 10, M = 1e5 + 10;

int fa[N], check[N];
int cnt;
int n, m;

struct node {
	int x, y, t;
}a[M];

bool cmp(node& x, node& y)
{
	return x.t < y.t;
}

int find(int x)
{
	return x == fa[x] ? x : fa[x] = find(fa[x]);
}
void un(int x, int y)
{
	int fx = find(x), fy = find(y);
	fa[fx] = fy;
}

int solve()
{
	for (int i = 1; i <= m; i++)
	{
		int x = a[i].x, y = a[i].y, t = a[i].t;
		if (check[x]++ == 0) cnt++;
		if (check[y]++ == 0) cnt++;
		un(x, y);
		if (cnt == n)
		{
			int ret = 0;
			for (int i = 1; i <= n; i++)
				if (fa[i] == i) ret++;
			if (ret == 1) return t;
		}
	}
	return -1;
}

int main()
{
	cin >> n >> m;
	for (int i = 1; i <= n; i++) fa[i] = i;
	for (int i = 1; i <= m; i++)
	{
		cin >> a[i].x >> a[i].y >> a[i].t;
	}
	sort(a + 1, a + 1 + m, cmp);
	cout << solve() << endl;
	return 0;
}
